﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using ZLCMS.Business;
using System.Collections;
using ZLCMS.Data.Model;
using NHibernate.Criterion;
using ZLCMS.Business.DwzFrame;
using System.Reflection;
using NHibernate;
using ZLCMS.Data.Common;
using ZLCMS.Business.UploadFileInfo;
using Newtonsoft.Json;
using ZLCMS.Business.Util;

namespace ZLCMS.Web.SystemManage.Basics.ProductSKU
{
    public partial class SKUGroupEdit : System.Web.UI.Page
    {
        protected FrameHelper _framePage;
        protected ShopSkuGroup _group;
        public string _userAccount;
        protected void Page_Load(object sender, EventArgs e)
        {
            _userAccount = UserKit.GetUserAccountFailLogin();
            if (!string.IsNullOrWhiteSpace(Request["kind"]))
                doc_kind.Value = Request["Kind"];
            if (!string.IsNullOrWhiteSpace(Request["cid"]))
                doc_cid.Value = Request["cid"];
            //初始化DWZ控件
            _framePage = new FrameHelper(this);
     
            //初始化全局变量值
            InitializeGlobalValue();

            //初始化控件值
            InitializeContent();

            if (IsPostBack)
            {
                Save();
            }
        }
        protected void InitializeGlobalValue()
        {
            int id;
            try
            {
                if (!IsPostBack)
                {
                    if (string.IsNullOrWhiteSpace(Request["uid"]))
                    {
                        ViewState["uid"] = null;
                        return;
                    }

                    id = int.Parse(Request["uid"]);
                    ViewState["uid"] = id;
                }
                else
                {
                    if (ViewState["uid"] == null)
                    {
                        return;
                    }
                    id = (int)ViewState["uid"];
                }

                using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    _group = session.CreateCriteria<ShopSkuGroup>().Add(Expression.Eq("Id", id)).UniqueResult<ShopSkuGroup>();
                }

                if (_group == null)
                {
                    throw new Exception("找不到对应信息.");
                }
            }
            catch (System.Exception ex)
            {
                _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                return;
            }
        }

        protected void InitializeContent()
        {
            ParamKit.Bind("YesNo_10", ddl_Status, null, null, null, "-1", "-1");

            if (!IsPostBack)
            {
                if (_group != null)
                {
                    groupName.Text = _group.SkuName;
                    chkColor.Checked = DatabaseHelper.IsTrue(_group.IsColor);
                    chkEnum.Checked = DatabaseHelper.IsTrue(_group.IsEnum);
                    chkMulti.Checked = DatabaseHelper.IsTrue(_group.IsMultiple);
                    chkMust.Checked = DatabaseHelper.IsTrue(_group.IsMust);
                    tb_showOrder.Text = _group.ShowOrder.ToString();
                    ddl_Status.SelectedValue = _group.Status;
                    PrivateName.Text = _group.PrivateName;
                    BindProduct(_group.Id);

                    ViewState["typeid"] = Request["typeid"];

                    using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                    {
                        try
                        {
                            string uid = Request["uid"];

                            if (!string.IsNullOrEmpty(uid))
                            {
                                doc_id.Value = uid;

                                ShopSkuGroup _productgroup = session.Get<ShopSkuGroup>(Int32.Parse(Request["uid"]));

                            }

                            doc_kind.Value = (String.IsNullOrEmpty(Request["Kind"]) ? "Default" : Request["Kind"]);

                        }
                        catch (System.Exception ex)
                        {
                            _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                        }
                    }
                }
                else
                {
                    ddl_Status.SelectedValue = "1";
                }
            }
        }
        protected void Save()
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                if (_group == null)
                {
                    _group = new ShopSkuGroup();
                    _group.SkuName = groupName.Text;
                    _group.IsColor = DatabaseHelper.ConvertBool(chkColor.Checked);
                    _group.IsEnum = DatabaseHelper.ConvertBool(chkEnum.Checked);
                    _group.IsMultiple = DatabaseHelper.ConvertBool(chkMulti.Checked);
                    _group.IsMust = DatabaseHelper.ConvertBool(chkMust.Checked);
                    _group.ShowOrder = int.Parse(tb_showOrder.Text);
                    _group.Status = ddl_Status.SelectedValue;
                    _group.CreateDate = DateTime.Now;
                    _group.PrivateName = PrivateName.Text;
                    if (!string.IsNullOrWhiteSpace(doc_cid.Value))
                        _group.CategoryId = int.Parse(doc_cid.Value);
                    session.Save(_group);
                }
                else
                {
                    _group.SkuName = groupName.Text;
                    _group.PrivateName = PrivateName.Text;
                    _group.IsColor = DatabaseHelper.ConvertBool(chkColor.Checked);
                    _group.IsEnum = DatabaseHelper.ConvertBool(chkEnum.Checked);
                    _group.IsMultiple = DatabaseHelper.ConvertBool(chkMulti.Checked);
                    _group.IsMust = DatabaseHelper.ConvertBool(chkMust.Checked);
                    _group.ShowOrder = int.Parse(tb_showOrder.Text);
                    _group.Status = ddl_Status.SelectedValue;
                    _group.CreateDate = DateTime.Now;

                    session.Update(_group);
                }

                session.Flush();
            }

            _framePage.Alert(new ReturnCode()
            {
                statusCode = 200,
                message = "操作成功!",
                callbackType = "forward",
                forwardUrl = "/SystemManage/Basics/ProductSKU/SKUGroupEdit.aspx?tabid=foodedit&uid=" + _group.Id
            });
        }
        protected void BindProduct(int shopId)
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                rp_shopaddr.DataSource = session.CreateCriteria<ShopSkuItem>().Add(Expression.Eq("SkuGroupId", shopId)).List<ShopSkuItem>();
                rp_shopaddr.DataBind();
            }
        }
    }
}
